包含min函数的栈 Posted on 2019-08-23 | In 剑指offer | | reads times 包含min函数的栈题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。 12345678910111213141516171819202122232425262728293031323334353637var stack=[];var minstack=[];var minele=null;function push(node){ // write code here if(minele!==null){ if(node<minele){ minele=node; } stack.push(node); minstack.push(minele); }else{ minele=node; stack.push(node); minstack.push(minele); }}function pop(){ // write code here stack.pop(); minstack.pop(); }function top(){ // write code here return stack[stack.length-1]; }function min(){ // write code here return minstack[minstack.length-1];} Post author: GoldMiner Xun Post link: https://goldminerxun.github.io/2019/08/23/%E5%89%91%E6%8C%87offer%20JavaScript%E7%89%88%20(20)/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.